package ezxpnstest;

import java.util.*;
import ezxpns.table.*;
import ezxpns.types.*;

public class TestTable
{
	public TestTable()
	{
		try
		{
			Table table=new Table();
			table.addColumn("Number",VLong.class,new VLong(0));
			HashMap<String,Value<?>> data;
			List<Bound<?>> list;
			HashMap<String,List<Bound<?>>> filter;
			ArrayList<Long> results;
			long id;
			data=new HashMap<String,Value<?>>();
			data.put("Number",new VLong(1301));
			table.addRecord(data);
			table.addColumn("String",VString.class,new VString("default"));
			table.renameColumn("Number","Long");
			data=new HashMap<String,Value<?>>();
			data.put("Long",new VLong(1301));
			data.put("String",new VString("test0"));
			table.addRecord(data);
			data.put("String",new VString("test3"));
			table.addRecord(data);
			data=new HashMap<String,Value<?>>();
			data.put("Long",new VLong(2509));
			data.put("String",new VString("test1"));
			table.addRecord(data);
			data.put("String",new VString("test2"));
			table.addRecord(data);
			data.put("String",new VString("test3"));
			table.addRecord(data);
			data.put("String",new VString("test4"));
			table.addRecord(data);
			data.put("String",new VString("test5"));
			table.addRecord(data);
			list=new ArrayList<Bound<?>>();
			list.add(new Bound<VLong>(new VLong(1301),new VLong(1301)));
			list.add(new Bound<VLong>(new VLong(2509),new VLong(2509)));
			filter=new HashMap<String,List<Bound<?>>>();
			filter.put("Long",list);
			list=new ArrayList<Bound<?>>();
			list.add(new Bound<VString>(new VString("test2"),new VString("test4")));
			filter.put("String",list);
			results=table.getView().filterRecords(filter);
			assert results.size()==4;
			id=results.get(0);
			assert ((VLong)table.getFieldValue(id,"Long")).value()==1301;
			assert ((VString)table.getFieldValue(id,"String")).value()=="test3";
			id=results.get(1);
			assert ((VLong)table.getFieldValue(id,"Long")).value()==2509;
			assert ((VString)table.getFieldValue(id,"String")).value()=="test2";
			id=results.get(2);
			assert ((VLong)table.getFieldValue(id,"Long")).value()==2509;
			assert ((VString)table.getFieldValue(id,"String")).value()=="test3";
			id=results.get(3);
			assert ((VLong)table.getFieldValue(id,"Long")).value()==2509;
			assert ((VString)table.getFieldValue(id,"String")).value()=="test4";
			try
			{
				data=new HashMap<String,Value<?>>();
				data.put("Long",new VString("fail"));
				table.addRecord(data);
				assert false;
			}
			catch(Exception e)
			{
				assert e.getMessage().compareTo("Record field value does not match type of column \"Long\"!")==0;
			}
			System.out.printf("Table Test OK!");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		catch(AssertionError e)
		{
			e.printStackTrace();
		}
	}
}
